package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketFeedback;
import com.cskaoyan.market.service.MarketAdminFeedbackService;
import com.cskaoyan.market.service.impl.MarketAdminFeedBackServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/feedback/*")
public class AdminFeedBackController extends HttpServlet {
    MarketAdminFeedbackService service = new MarketAdminFeedBackServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace("/admin/feedback/", "");

        if ("list".equals(op)) {
            list(req, resp);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        String username = req.getParameter("username");

        Integer id;
        if (StringUtils.isEmpty(req.getParameter("id"))) {
            id = null;
        }else {
            id = Integer.valueOf(req.getParameter("id"));
        }

        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        List<MarketFeedback> feedbacks = service.list(page, limit, username, id, sort, order);

        Object o = ResponseUtil.okList(feedbacks);
        String s = JacksonUtil.writeValueAsString(o);

        resp.getWriter().println(s);
    }
}
